package wx

import (
	"errors"

	"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
	"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount"

	"gin-api-server/core_library/global"
)

func InitWxMp() error {
	config := global.Config.Wechat.Mp
	if config.Appid == "" {
		return nil
	}
	redisConf := global.Config.Redis

	OfficialAccountApp, err := officialAccount.NewOfficialAccount(&officialAccount.UserConfig{
		AppID:  config.Appid,  // 公众号、小程序的appid
		Secret: config.Secret, //
		Log: officialAccount.Log{
			Level: "debug",
			File:  global.RootPath + "/runtime/logs/wechat-mp.log",
		},
		AESKey:    config.Secret,
		HttpDebug: false,
		Debug:     false,
		Cache: kernel.NewRedisClient(&kernel.RedisOptions{
			Addr:     redisConf.Host + ":" + redisConf.Port,
			Password: redisConf.Password,
			DB:       redisConf.Db,
		}),
	})

	if err != nil {
		return errors.New("初始化 wechat mp 失败:" + err.Error())
	}
	global.WechatMp = OfficialAccountApp
	return err
}
